<template>
  <el-menu
    :default-active="openIndex"
    class="el-menu-vertical-demo"
    router
    text-color="#333"
    active-text-color="#fff"
    @select="selectMenu"
  >
    <template v-for="item in menuArr">
      <el-menu-item
        :index="item.path"
        class="menu-item first-item"
        :route="item.path"
        :key="item.id"
        @click="setTabs(item)"
        v-if="!item.children && item.menuType == 1"
      >
        <i :class="item.icon"></i><span>{{ item.menuName }}</span></el-menu-item
      >
      <el-submenu
        :index="item.menuName"
        :key="item.id"
        v-else-if="
          item.children && item.children.length > 0 && item.menuType == 1
        "
      >
        <template slot="title">
          <i :class="item.icon" class="top-icon"></i>
          <span>{{ item.menuName }}</span>
        </template>

        <template v-for="items in item.children">
          <el-menu-item
            v-if="items.menuType == 1"
            :key="items.id"
            class="menu-item"
            :index="items.path"
            @click="setTabs(items)"
          >
            <i class="el-icon-arrow-right"></i>
            <span slot="title">{{ items.menuName }}</span></el-menu-item
          >
        </template>
      </el-submenu>
    </template>
  </el-menu>
</template>
<script>
import storage from "../../utils/storage";

import { mapMutations } from "vuex";
export default {
  name: "treeMenu",
  components: {},
  props: {
    menuArr: Array,
    nowNav: String,
  },
  data() {
    return {
      openIndex: "/home/first",
    };
  },
  methods: {
    ...mapMutations("home", ["addTabs", "closeTabs"]),
    setTabs(item) {
      this.$emit("chageVal", item.path);
      this.addTabs(item);
    },
    selectMenu(key) {
      this.openIndex = key;
      this.$emit("nowMenu", key);
    },

    breakPage(e) {
      storage.setStorage("nowNav", this.openIndex);
    },
  },
  computed: {},
  watch: {},
  created() {
    this.openIndex = sessionStorage.getItem("nowNav") || "/home/first";
    window.addEventListener("beforeunload", (e) => this.breakPage(e));
  },
  destroyed() {
    window.removeEventListener("beforeunload", (e) => this.breakPage(e));
  },
};
</script>
<style lang="less" scoped>
.el-menu-item {
  line-height: 48px !important;
  height: 48px;
  margin: 12px 0;
  border-radius: 24px;
  box-sizing: border-box;
  width: 100%;
}
.el-submenu .el-menu-item {
  min-width: 160px !important;
}
.el-menu-item /deep/ i {
  font-size: 14px;
}
.first-item {
  padding: 0 !important;
}

.el-menu-vertical-demo {
  padding: 12px 24px !important;
}
.el-menu-vertical-demo /deep/ .first-item:first-child {
  background-color: #f2f4f6;
  margin: 24px 0 !important;
  padding: 0 !important;
  text-align: center;
}
.el-submenu.is-active /deep/ .el-submenu__title {
  color: #41b6e6;
}
.el-submenu /deep/ .top-icon {
  width: 14px;
  margin-right: 5px;
  padding: 0 5px;
}
.el-submenu.is-active /deep/ .el-submenu__title .top-icon,
.el-submenu.is-active /deep/ .el-submenu__title span {
  color: #41b6e6;
}
.el-submenu /deep/ .el-submenu__title:hover {
  background-color: transparent !important;
  color: #0a9bd4 !important;
  .top-icon {
    color: #0a9bd4 !important;
  }
}
.menu-item {
  padding-left: 60px !important;
  border-radius: 24px;
  //   color: #333 !;
}
.menu-item.is-active {
  background-color: #41b6e6 !important;
  color: #fff !important;
}
</style>
